/*-----------------------------------------------------------------------------------
 *
 * 프로그램명	: CommonDAO.java
 * Version		: 1.0
 * 작성일 		: 2006-08-16
 * 작성자		:
 * 수정일 		:
 * 수정자		:
 * 설명		 	:
 * 기능		 	:
 *
 * Copyright (c) 2000 by H&H  All Rights Reserved.
 *
-------------------------------------------------------------------------------------*/
package com.joinhawaii.common;

import java.io.*;

import java.util.*;
import java.sql.*;
import javax.servlet.http.*;

import com.zll.framework.db.DBManager;
import com.zll.framework.model.HashList;
import com.zll.framework.util.StringUtil;



public class CommonUtil
{
	/** 공통코드 싱글톤 */
	private static HashMap codeMap = new HashMap();

	public static void reloadCode() {
		codeMap = new HashMap();
	}

	public static HashList getCodeGubn(String code_gubn, String code_name) throws Exception
	{
		HashMap infos = new HashMap();
		infos.put("code_gubn", code_gubn);
		infos.put("code_name", code_name);
		return DBManager.executeQuery("/common.xml", "select_code_list", infos);
	}

	public static HashList getCodeList(String code_gubn) throws Exception
	{
		return getCodeList(code_gubn, null);
	}


	public static HashList getCodeList(String code_gubn, String all_name) throws Exception
	{
		HashMap infos = new HashMap();
		infos.put("code_gubn", code_gubn);
		infos.put("all_name", all_name);
		infos.put("use_yn", "Y");
		return getCodeList(infos);
	}

	public static HashList getCodeList(HashMap infos) throws Exception
	{
		HashList list = null;

		String code_gubn 	= (String)infos.get("code_gubn");
		String all_name		= (String)infos.get("all_name");

		if ("hotel".equals(code_gubn)) {
			list = DBManager.executeQuery("/common.xml", "select_hotel_list", infos);
		}
		else if ("room".equals(code_gubn)) {
			list = DBManager.executeQuery("/common.xml", "select_room_list", infos);
		}
		else if ("enjoy".equals(code_gubn)) {
			list = DBManager.executeQuery("/common.xml", "select_enjoy_list", infos);
		}
		else if ("price".equals(code_gubn)) {
			list = DBManager.executeQuery("/common.xml", "select_enjoy_price_list", infos);
		}
		else if ("car".equals(code_gubn)) {
			list = DBManager.executeQuery("/common.xml", "select_car_list", infos);
		}
		else  {
			if (codeMap.get(code_gubn) == null)
			{
				if ("use_yn".equals(code_gubn))
				{
					list = new HashList();

					HashMap map = new HashMap();
					map.put("code", "Y");
					map.put("code_name", "사용");
					list.add(map);

					map = new HashMap();
					map.put("code", "N");
					map.put("code_name", "미사용");
					list.add(map);
				}
				else
				{
					list = DBManager.executeQuery("/common.xml", "select_code_list", infos);
				}

				//싱글톤 코드에 저장.
				codeMap.put(code_gubn, list);
			}
			else {
				list = (HashList)codeMap.get(code_gubn);
			}


		}

		if (all_name == null) {
			return list;
		} else {
			HashList allList = new HashList();
			HashMap map = new HashMap();
			map.put("code", "");
			map.put("code_name", all_name);
			map.put("option_group", "");
			allList.add(map);
			allList.add(list);

			return allList;
		}
	}

	public static HashList getServiceAction(String service_code) throws Exception
	{
		HashMap infos = new HashMap();
		infos.put("service_code", service_code);
		return DBManager.executeQuery("/common.xml", "select_service_action_list", infos);
	}

	public static HashMap getCode(String dept_gbn, String code_gubn, String code) throws Exception
	{
		HashMap infos = new HashMap();
		infos.put("dept_gbn", 	dept_gbn);
		infos.put("code_gubn", 	code_gubn);
		infos.put("code",		code);
		HashList HashList = DBManager.executeQuery("/common.xml", "select_code_list", infos);
		if (HashList.size() == 1) {
			return (HashMap)HashList.get(0);
		} else {
			return new HashMap();
		}
	}


	public static String nextDay(String from_date, int cnt) throws Exception
	{
		HashMap infos = new HashMap();
		infos.put("from_date", from_date);
		infos.put("cnt", 	String.valueOf(cnt));
		HashList cList = DBManager.executeQuery("/common.xml", "select_next_day", infos);
		return cList.get("to_date", 0);
	}













	public static String getCodeSelect(String code_gubn, String select_name, String select_value, int width) throws Exception
	{
		HashMap infos = new HashMap();
		infos.put("code_gubn", code_gubn);
		infos.put("use_yn", "Y");
		HashList list = DBManager.executeQuery("/common.xml", "select_code_list", infos);

		StringBuffer sb = new StringBuffer();
		sb.append("<select name=\""+select_name+"\" style=\"width:"+width+"\">");
		for (int i = 0; i < list.size(); i++) {
			sb.append("<option value=\""+list.get("code",i)+"\" " + StringUtil.selected(list.get("code",i),select_value)+">"+list.get("code_name",i)+"</option>");
		}
		sb.append("</select>");

		return sb.toString();
	}


	public static void insertLog(HttpServletRequest request) throws Exception
	{
		HashMap infos = new HashMap();
		infos.put("main_path",		StringUtil.nvl(new String(request.getRequestURL())));
		infos.put("user_name", 		StringUtil.nvl(request.getSession().getAttribute("session.user_name")));
		infos.put("referer",		StringUtil.nvl(request.getHeader("referer")));
		infos.put("ip_addr",		StringUtil.nvl(request.getRemoteAddr()));
		infos.put("reg_user", 		StringUtil.nvl(request.getSession().getAttribute("session.user_id")));


		DBManager.update("/common.xml", "insert_log", infos);
	}

	public static String toRegExp(String[] params) {
		String infos = "";
		if (params != null) {
			for (int i = 0; i < params.length; i++) {
				infos += params[i];
				if (i < params.length-1)	infos += "|";
			}
		}
		return infos;
	}


	public static String getCheckBox(String code_gubn, HashList list, String values) {

		if (list == null) {
			return "";
		}
		StringBuffer html = new StringBuffer();
		String checked = "";
		for (int i = 0; i < list.size(); i++) {
			if (values.indexOf(list.get("code",i)) > -1) {
				checked = " checked ";
			} else {
				checked = "";
			}
			html.append("<input type=\"checkbox\"")
				.append(" name=\"s_").append(code_gubn).append("\"")
				.append(checked)
				.append(" value=\"").append(list.get("code",i)).append("\">")
				.append(list.get("code_name",i)).append("\n");
		}
		return html.toString();
	}

	public static String getCheckBox(String code_gubn, HashMap map, String values) {
		return getCheckBox(code_gubn, (HashList)map.get(code_gubn), values);
	}
}
